﻿using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;
using BCEntity.TMS.RedPacket.RedPacketSupplement;

namespace BCData.TMS.RedPacket.RedPacketSupplement
{
    public class RedPacketSupplementData : IRedPacketSupplementData
    {
        private readonly IDatabaseContext database;
        private readonly string QUERY = @"SELECT
		                                    RedPacketSupplementId,
		                                    RedPacketId,
		                                    RedPacketTypeId,
		                                    RedPacketType,
		                                    RedPacketAmount,
		                                    RedPacketName,
		                                    RedPacketCount,
		                                    AgentRecordId,
		                                    CreateTime,
		                                    CreateAdminId,
		                                    CreateAdminName
                                        FROM
                                            RedPacketSupplement";

        private readonly string SQL_QUERY_VIEW = @"SELECT
		                                    A.RedPacketSupplementId,
		                                    A.RedPacketId,
		                                    A.RedPacketTypeId,
		                                    A.RedPacketType,
		                                    A.RedPacketAmount,
		                                    A.RedPacketName,
		                                    A.RedPacketCount,
		                                    A.AgentRecordId,
		                                    A.CreateTime,
		                                    A.CreateAdminId,
		                                    A.CreateAdminName
                                        FROM
                                            RedPacketSupplement A";

        #region 构造函数
        public RedPacketSupplementData(IDatabaseContext database)
        {
            this.database = database;
        }
        #endregion

        #region InsertAsync
        /// <summary>
        /// 添加新红包补充记录纪录
        /// </summary>
        /// <param name="redPacketSupplementEntity"></param>
        /// <returns></returns>
        public async Task<RedPacketSupplementEntity> InsertAsync(RedPacketSupplementEntity entity)
        {
            return await this.database.Master.InsertAsync(entity, database.Transaction);
        }
        #endregion

        #region updateAsync
        /// <summary>
        /// 修改红包补充记录记录
        /// </summary>
        /// <param name="redPacketSupplementEntity"></param>
        /// <returns></returns>
        public async Task<RedPacketSupplementEntity> UpdateAsync(RedPacketSupplementEntity entity)
        {
            return await this.database.Master.UpdateAsync(entity, database.Transaction);
        }
        #endregion 

        #region Delete
        /// <summary>
        /// 删除红包补充记录记录
        /// </summary>
        /// <param name="redPacketSupplementId"></param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(long redPacketSupplementId)
        {
            string sql = "DELETE FROM redpacketsupplement WHERE RedPacketSupplementId = ?redPacketSupplementId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?RedPacketSupplementId", redPacketSupplementId, DbType.Int64);
            return await this.database.Master.ExecuteAsync(sql, parameters, database.Transaction) > 0;
        }
        #endregion

        #region GetById
        /// <summary>
        /// 获取红包补充记录记录
        /// </summary>
        /// <param name="redPacketSupplementId"></param>
        /// <returns></returns>
        public async Task<RedPacketSupplementEntity> GetEntityAsync(long redPacketSupplementId)
        {
            string sql = QUERY + " WHERE  RedPacketSupplementId = ?redPacketSupplementId";

            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?RedPacketSupplementId", redPacketSupplementId, DbType.Int64);
            return await this.database.Slave.QueryEntityAsync<RedPacketSupplementEntity>(sql, parameters);
        }
        #endregion

        #region GetListAsync 查询红包补充记录记录
        public async Task<(IEnumerable<RedPacketSupplementEntity>, int)> GetListAsync(long agentRecordId, long? redPacketId, 
            int pageIndex, int pageSize)
        {
            DynamicParameters parameters = new DynamicParameters();
            //查询条件
            List<string> wheres = new List<string>();
            wheres.Add(" A.AgentRecordId= ?AgentRecordId ");
            parameters.Add("?AgentRecordId", agentRecordId, DbType.Int64);
            if (redPacketId.HasValue)
            {
                wheres.Add(" A.RedPacketId= ?RedPacketId ");
                parameters.Add("?RedPacketId", redPacketId.Value, DbType.Int64);
            }
            string whereSql = string.Empty;
            if (wheres.Count > 0)
            {
                whereSql = $"WHERE {string.Join(" AND ", wheres)} ";
            }
            var sqlCount = " SELECT COUNT(A.RedPacketSupplementId) FROM RedPacketSupplement A ";
            string sql = $@"{SQL_QUERY_VIEW} {whereSql} GROUP BY A.RedPacketSupplementId 
                                Order By A.CreateTime DESC LIMIT {(pageIndex - 1) * pageSize}, {pageSize};
                            {sqlCount}{whereSql}";
            return await this.database.Slave.QueryPageableListAsync<RedPacketSupplementEntity>(sql, parameters);
        }
        #endregion

    }
}
//接口



